package com.graduate.back.component;

import com.graduate.back.pojo.MyUser;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author bwzfy
 * @create 2021/5/16
 **/
@Component
public class SuccessHandler implements AuthenticationSuccessHandler {
    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        // 认证成功之后会执行的方法
        MyUser user = (MyUser) authentication.getPrincipal();
        request.getSession().setAttribute("user", user);
        response.getWriter().write("ok");
    }
}
